﻿using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web
{
    public partial class Cart : System.Web.UI.Page
    {
        public string CartList { get; set; }
        private CartDao CartDao = new CartDao();

        protected void Page_Load(object sender, EventArgs e)
        {
            // 验证当前是否有用户登录
            Account account = (Account)Session["account"];
            // 未登录则禁止访问本页，跳转到登录页面
            if (account == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                // 通过用户ID获取订单
                List<Model.Cart> list = CartDao.GetCartByAccountId(account.Id);

                ReflashPage(list);
            }
        }

        /// <summary>
        /// 删除一条购物车记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnDeleteCart_Click(object sender, EventArgs e)
        {

            if (CartDao.DeleteCartById(Convert.ToInt32(CartId.Text)))
            {
                // 刷新当前页
                Response.AddHeader("Refresh", "0");
            }

        }

        protected void BtnUpdate_Click(object sender, EventArgs e) 
        {
            Model.Cart cart = new Model.Cart();
            cart.Id = Convert.ToInt32(CartId.Text);
            cart.Num = Convert.ToInt32(NewNum.Text);
            if (CartDao.UpdateCart(cart))
            {
                Account account = (Account)Session["account"];
                // 通过用户ID获取订单
                List<Model.Cart> list = CartDao.GetCartByAccountId(account.Id);
                // 刷新当前页
                ReflashPage(list);
            }
        }

        protected void BtnOrder_Click(object sender, EventArgs e)
        {
            string id = CartId.Text;
            Response.Redirect("Ordering.aspx?id="+id);
        }


        protected void ReflashPage(List<Model.Cart> list) {
            if (list != null)
            {
                // 套接字符串
                StringBuilder sbBuilder = new StringBuilder();
                System.Diagnostics.Debug.Write(list.Count());
                foreach (Model.Cart cart in list)
                {
                    //// 生成html
                    //sbBuilder.AppendFormat(
                    //    "<tr><td>{0}</ td><td><img alt = '' class='book_image' src='../{1}'/></td><td class='text-primary'>" +
                    //    "<a href='BookDetial.aspx?id={6}'>{2}</a></td><td class='text-success'>{3}</td>" +
                    //    "<td class='text-danger'>￥{4}</td><td class='text-danger'>{5}</td><td>" +
                    //    "<input class='btn btn-danger delete_book' data-toggle='modal' data-target='#confirm-delete' bookId='{6}' bookName='{2}' value='删除' type='button' />" +
                    //    "</td></tr>", cart.Id, cart.Book.Image,cart.Book.Name, cart.Book.Oneword,cart.Book.Price, cart.Num,cart.Book.Id);

                    sbBuilder.AppendFormat(
                        "<div class ='page_bookDetail' >" +
                        "<div class='row'>" +
                        " <div class='col-md-3 clean'><img alt = '商品预览图' class='book_img' src='../Book_Image/{1}' /></div>" +
                            "<div class='col-md-8'><div class='row'><div class='col-md-offset-1'>" +
                            "<h2>《{2}》</h2><h4><font color='#D5D5D5'>推荐理由：</font>{3}</h4>" +
                                        "<hr class='bg-info' /><blockquote class='bg-warning text-danger'>" +
                                           "<h5 class='text-primary'>当前价格</h5>￥<font size = '7' >{4}</font ></blockquote>" +
                                        "<div class='row'>" +
                                            "<div class='col-md-2'><font size = '5' > 数  量：</font></div>" +
                                           " <div class='col-md-3'>" +
                                             "<div class='row'>" +
                                                 "<div class='col-md-9'>" +
                                                    "<div class='input-group'>" +
                                                       "<span class='input-group-btn'>" +
                                                               "<input type = 'button' class='btn btn-default btn_reduce' value=' - ' />" +
                                                            "</span>" +
                                                             "<span><input type='text' class='form-control num' value='{5}' /></span>" +
                                                             "<input type='text' class='form-control cartid' style='display: none; ' value='{0}' />" +
                                                            "<span class='input-group-btn'>" +
                                                               "<input type = 'button' class='btn btn-default btn_add' value='+' />" +
                                                            "</span>" +
                                                        "</div>" +
                                                    "</div>" +
                                                "</div>" +
                                           "</div>" +
                                            "<div class='col-md-5'>" +
                                            "<input type='text' class='form-control' style='display: none; ' value='{0}' />" +
                                               "<input type = 'button' class='btn btn-danger btnToOrder' value='去下单'/>" +
                                            "</div>" +
                                        "</div>" +
                                        "<br/>" +
                                        "<div" +
                                            "<lable class='text-danger' id='errorInfo' value=''></label>" +
                                        "</div>" +
                                    "</div>" +
                                "</div>" +
                           " </div>" +
                        "</div>" +
                    "</div>", cart.Id, cart.Book.Image, cart.Book.Name, cart.Book.Oneword, cart.Book.Price, cart.Num, cart.Book.Id);
                }
                // 返回给浏览器
                CartList = sbBuilder.ToString();
            }
            else
            {
                EmptyInfo.Text = "您的购物车里还没有东西哦～";
            }
        }
        
    }

}